package com.spbdx.web.controller.pc.system;

import com.alibaba.fastjson2.JSONObject;
import com.spbdx.common.annotation.Log;
import com.spbdx.common.core.controller.BaseController;
import com.spbdx.common.core.domain.AjaxResult;
import com.spbdx.common.core.page.TableDataInfo;
import com.spbdx.common.enums.BusinessType;
import com.spbdx.common.utils.poi.ExcelUtil;
import com.spbdx.system.domain.EventOrder;
import com.spbdx.system.domain.EventOrderFlow;
import com.spbdx.system.service.IEventOrderFlowService;
import com.spbdx.system.service.IEventOrderService;
import com.spbdx.system.service.workflow.StateEventUtil;
import com.spbdx.system.service.workflow.support.MachineStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工单流转Controller
 *
 * @author cms
 * @date 2024-08-07
 */
@Api(tags = "工单流转")
@RestController
@RequestMapping("/anchang/order")
public class EventOrderFlowController extends BaseController {
    @Autowired
    private IEventOrderFlowService eventOrderFlowService;


    @Resource
    private IEventOrderService eventOrderService;

    @Resource
    private StateEventUtil stateEventUtil;

    @ApiOperation("创建工单，测试用")
    @PostMapping("/create")
    public void createOrder() {
        EventOrder eventOrder = new EventOrder();
        eventOrder.setOrderStatus(MachineStatus.NEW.getCode());
        eventOrder.setType("machine");
        eventOrderService.insertEventOrder(eventOrder);
    }

    @ApiOperation("工单状态转换，测试用")
    @PutMapping("/convert")
    public void test(Long id, String event) {
        JSONObject object = new JSONObject();
        object.put("id", id);
        stateEventUtil.sendEvent(id, event, object);
    }

    /**
     * 查询工单流转表列表
     */
    @ApiOperation("查询工单流转表列表")
    @GetMapping("/flow/list")
    public TableDataInfo list(EventOrderFlow eventOrderFlow) {
        startPage();
        List<EventOrderFlow> list = eventOrderFlowService.selectEventOrderFlowList(eventOrderFlow);
        return getDataTable(list);
    }

    /**
     * 导出工单流转表列表
     */
    @Log(title = "工单流转表", businessType = BusinessType.EXPORT)
    @PostMapping("/flow/export")
    public void export(HttpServletResponse response, EventOrderFlow eventOrderFlow) {
        List<EventOrderFlow> list = eventOrderFlowService.selectEventOrderFlowList(eventOrderFlow);
        ExcelUtil<EventOrderFlow> util = new ExcelUtil<EventOrderFlow>(EventOrderFlow.class);
        util.exportExcel(response, list, "工单流转表数据");
    }

    /**
     * 获取工单流转表详细信息
     */
    @ApiOperation("获取工单流转表详细信息")
    @GetMapping(value = "/flow/{flowId}")
    public AjaxResult getInfo(@PathVariable("flowId") Long flowId) {
        return success(eventOrderFlowService.selectEventOrderFlowByFlowId(flowId));
    }

}